<!-- 编辑弹窗 -->
<template>
  <ele-modal
    inner
    fullscreen
    :visible="visible"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="'库存列表'"
    @update:visible="updateVisible" :before-close="handleClose"
  >
    <div class="ele-body">
      <purorderSearch @search="reload"  />
      <el-card>
      <!-- 搜索表单 -->
      <el-row :gutter="24">
        <el-col  :md="13" :sm="24">
          <ele-pro-table ref="table"  row-key="id"  sub-title="未选库存:" :toolkit="[]"  :columns="allorderColumns"  :datasource="datasource"   @done="onDone" :need-page="true"  :height="tableHeight" :full-height="tableFullHeight">
            <template v-slot:toolkit>
              <el-button size="mini" class="ele-btn-icon" @click="addall">
                全部添加
              </el-button>
            </template>
            <template v-slot:action="{ row }">
              <el-button size="mini" @click="add(row)">添加</el-button>
            </template>
          </ele-pro-table>
        </el-col>
        <el-col  :md="11" :sm="24">
          <ele-pro-table ref="choosetable"  row-key="id,checkContainerNumberItems"   sub-title="已选库存:" :toolkit="[]"   :columns="chooseOrderColumns"  :datasource="chooseDatas"   :need-page="false"  :height="tableHeight" :full-height="tableFullHeight">
            <template v-slot:toolkit>
              <el-button size="mini" class="ele-btn-icon" @click="removeAll" type="danger">
                全部删除
              </el-button>
            </template>
            <template v-slot:action="{ row }">
              <el-button size="mini" type="danger" @click="remove(row)">删除</el-button>
            </template>
            <template slot="costPrice" slot-scope="{ row }">
              <el-select  filterable v-model="row.costPrice" class="ele-fluid" >
                <el-option v-for="item in row.costPriceList" :key="item" :label="item" :value="item" />
              </el-select>
            </template>
            <template slot="ss" slot-scope="{ row }">
              <span>{{ row.checkContainerNumberItems.join(",") }}</span>
            </template>
          </ele-pro-table>
        </el-col>
      </el-row>
      
      </el-card>
    </div>
    <div slot="footer">
      <el-button type="primary" :loading="loading" @click="save" >
        确定
      </el-button>
    </div>
  </ele-modal>
</template>

<script>
  
  import purorderSearch from './purchoose-search.vue';
  import { QueryStockByCondition } from '@/api/inventory/stock';
 
  export default {
    name: 'purChooseList',
    components: {purorderSearch},
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
    },
    data() {
      return {
        fixedHeight:true,
        // 表单数据
        form: {},
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
        chooseDatas:[],
        // 表格列配置
        allorderColumns: [
          { columnKey: 'action',label: '操作',width: 80,align: 'center',slot: 'action'},
          // {prop: 'supplierDesc', label: '供应商', showOverflowTooltip: true,align: 'center',},
          {prop: 'productCode', label: '产品编号', showOverflowTooltip: true, align: 'center',},
          {prop: 'productName', label: '产品名称', showOverflowTooltip: true, align: 'center',},
          {prop: 'spc', label: '规格', showOverflowTooltip: true, align: 'center',},
          {prop: 'unitDesc', label: '单位', showOverflowTooltip: true, align: 'center',},
          {prop: 'stockQty', label: '库存数量', showOverflowTooltip: true, align: 'center',},
          {prop: 'useQty', label: '可用数量', showOverflowTooltip: true, align: 'center',},
          {prop: 'costPrice', label: '成本单价',showOverflowTooltip: true, align: 'center',},
        ],
        // 表格列配置
        chooseOrderColumns: [
          {columnKey: 'action',label: '操作',width: 80,align: 'center',slot: 'action'},
          // {prop: 'supplierDesc', label: '供应商', showOverflowTooltip: true,align: 'center',},
          {prop: 'productCode', label: '产品编号', showOverflowTooltip: true, align: 'center',},
          {prop: 'productName', label: '产品名称', showOverflowTooltip: true, align: 'center',},
          {prop: 'spc', label: '规格', showOverflowTooltip: true, align: 'center',},
          {prop: 'unitDesc', label: '单位', showOverflowTooltip: true, align: 'center',},
          {prop: 'stockQty', label: '库存数量', showOverflowTooltip: true, align: 'center',},
          {prop: 'useQty', label: '可用数量', showOverflowTooltip: true, align: 'center',},
          {prop: 'costPrice',slot:'costPrice', label: '成本单价',showOverflowTooltip: true, align: 'center',},
        ],
      };
    },

    computed: {
      // 表格高度
      tableHeight() {

        var fh = this.fixedHeight ? 'calc(100vh - 380px)' : void 0;
        return fh;
      },
      // 表格全屏时高度
      tableFullHeight() {
        var fh =  this.fixedHeight ? 'calc(100vh - 280px)' : null;
        return fh;
      },
    },
   created(){
   },
    methods: {
      
      /* 表格数据源 */
      async datasource(params) {
        
        params.where.pageindex = params.page;
        params.where.pagesize = params.limit;

        //添加排序
        if(params.order.order == null || params.order.order == undefined){
          params.where.orderBys = [{ sort: "createDate", order: 1, }];
        }else{
          params.where.orderBys = [];
          params.where.orderBys.push({sort:params.sorter.column.sortable,order:params.order.order=='asc'?0:1});
        }

        if(!params.where.conditions){
          params.where.conditions = {};
        }

        if(!params.where.conditions.wareHouseId){
          params.where.conditions.wareHouseId = this.data.wareHouseId;
        }

        let data = await QueryStockByCondition(params.where);
        return {
            list:data.items,
            count:data.totalrecords,
        };
      },

      save(){
        
        if(this.chooseDatas.length==0){
          this.$message({
            message: '没有选择库存',
            type: 'warning'
          });
        }else{
          this.$emit('callback',this.chooseDatas );
          this.$emit('update:visible', false);
        }
      },

      /* 表格渲染完成回调 */
      onDone({ data }) {

      },
      /* 刷新表格 */
      reload(where) {
        if(this.$refs.table){
          this.$refs.table.reload({ where: where });
        }
      },

      /* 更新visible */
      updateVisible(value) {
        //更新父组件中的visible的值
        this.$emit('update:visible', value);
        this.chooseDatas=[];
      },

      handleClose() {
        //更新父组件中的visible的值
        this.$emit('update:visible', false);
        this.chooseDatas=[];
      },

       /* 添加所有数据 */
      addall(){
       
        if(this.$refs.table.tableData!=null && this.$refs.table.tableData!=undefined && this.$refs.table.tableData.length>0){
          this.$refs.table.tableData.map((m)=>{
            this.chooseDatas.push(m);
          })
        }
      },
      
      /* 添加 */
      add(row) {
        if(this.chooseDatas.length>0){
          let itemIndex = this.chooseDatas.findIndex((f)=>{
            return f.id == row.id
          });

          if(itemIndex<0){
            this.chooseDatas.push(row)
          }else{
            this.$message.info("当前库存已经添加")
          }
        }else{
          this.chooseDatas.push(row);
        }
      },
      /* 移除 */
      remove(row) {
        this.chooseDatas.splice(this.chooseDatas.indexOf(row), 1);
      },
      /* 移除所有 */
      removeAll() {
        this.chooseDatas.splice(0, this.chooseDatas.length);
      }

    },
    /* 监听visible属性变化 */
    watch: {
      visible(visible) {
        this.chooseDatas=[];
        if(this.$refs.table){
            this.reload();
          }
      }
    }
  };
</script>
<style scoped>

  
  </style>